package com.wanggsx.library.base.views.numberbar;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wanggsx.library.R;

public class MyNumberBar extends RelativeLayout {
    private int mNumber = 0;
    private int mMin = 0;
    private int mMax = 1000;
    //递减按钮
    public TextView mReduceView;
    //递增按钮
    public TextView mAddView;
    //数字内容
    public TextView mTvContent;

    public MyNumberBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
     }

    public interface OnNumberChangedListener{
        void onChanged(int number, int preNum);
    }

    private OnNumberChangedListener mOnNumberChangedListener;
    public void setOnNumberChanged(OnNumberChangedListener onChanged){
        this.mOnNumberChangedListener = onChanged;
    }

    @Override
    final public void setOnClickListener(OnClickListener l) {

    }

    private void init(){
        View barView = LayoutInflater.from(getContext()).inflate(R.layout.number_bar,this,false);
        addView(barView);
        mReduceView = barView.findViewById(R.id.tvReductNumber_number_bar);
        mAddView = barView.findViewById(R.id.tvAddNumber_number_bar);
        mTvContent = barView.findViewById(R.id.tvNumber_number_bar);
        mReduceView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mNumber>=mMin+1){
                    mTvContent.setText(--mNumber + "");
                    if (mOnNumberChangedListener!=null){
                        mOnNumberChangedListener.onChanged(mNumber,mNumber+1);
                    }
                }
                if (mNumber==mMin){
                    mReduceView.setEnabled(false);
                    mReduceView.setAlpha(0.5f);
                }
            }
        });
        mAddView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //如果有有效最大值时不能超过最大值，否则可以无限增长
                if ((mMax>mMin && mNumber < mMax) || mMax < mMin){
                    mTvContent.setText(++mNumber + "");
                        if (mNumber==mMin+1){
                            mReduceView.setEnabled(true);
                            mReduceView.setAlpha(1.0f);
                        }
                    if (mOnNumberChangedListener!=null){
                        mOnNumberChangedListener.onChanged(mNumber,mNumber-1);
                    }
                }
            }
        });
        setNumber(0);
    }

    /**
     * 设置数字
     * @param number
     */
    public void setNumber(int number){
        if (number>=mMin){
            this.mNumber = number;
            this.mTvContent.setText(this.mNumber + "");
            if (mNumber==mMin){
                mReduceView.setEnabled(false);
                mReduceView.setAlpha(0.5f);
            }else{
                mReduceView.setEnabled(true);
                mReduceView.setAlpha(1.0f);
            }
        }
    }

    /**
     * 设置最小值
     * @param min
     */
    public void setMin(int min){
        if (min >= mMax){
            //UtilsToast.showShort("最小值不能大于最大值");
        }
        this.mMin = min;
        if (mNumber < min){
            setNumber(min);
        }
    }

    /**
     * 设置最大值
     * @param max
     */
    public void setMax(int max){
        if (max <= mMin){
            //UtilsToast.showShort("最大值不能小于最小值");
        }
        this.mMax = max;
        if (mNumber > max){
            setNumber(max);
        }
    }

    /**
     * 获取已选数字
     * @return
     */
    public int getNumber(){
        return this.mNumber;
    }
}
